// ['eq','ne','lt','le','gt','ge','bw','bn','in','ni','ew','en','cn','nc']
// The corresponding texts are in language file and mean the following:
// ['equal','not equal', 'less', 'less or equal','greater','greater or equal', 'begins with','does not begin with','is in','is not in','ends with','does not end with','contains','does not contain'] 

(function($){
    $.fn.extend({
        acc_search:function(options) {
            var defaults = {  
                foreground: 'red',  
                background: 'yellow'  
            }; 

            var opts = $.extend(defaults, options);
            var e_key = "<div class='voch_query_key'>__txt_key__</div>";
            var e_comp_int = "" +
                "<div class='voch_query_compare'>" +
                    "<select id='id_qcmp_select' class='ui-widget-content ui-corner-all acs_getcomp'>" + 
                        "<option value='=' aria-alias='eq' >等于</option>" +  
                        "<option value='>' aria-alias='gt' >大于</option>" + 
                        "<option value='<' aria-alias='lt' >小于</option>" + 
                    "</select>" + 
                "</div>";
            var e_comp_select = "" +
                "<div class='voch_query_compare'>等于</div>";
            var e_comp_text = "" +
                "<div class='voch_query_compare'>" +
                    "<select id='id_qcmp_select' class='ui-widget-content ui-corner-all acs_getcomp'>" + 
                        "<option value='cn' aria-alias='cn' selected='selected' >包含</option>" +  
                        "<option value='nc' aria-alias='nc' >不包含</option>" + 
                        "<option value='bw' aria-alias='bw' >以开头</option>" + 
                        "<option value='bn' aria-alias='bn' >不以开头</option>" + 
                        "<option value='ew' aria-alias='ew' >以结尾</option>" + 
                        "<option value='en' aria-alias='en' >不以结尾</option>" + 
                    "</select>" + 
                "</div>";

            var e_v_text = "" +
                "<div class='voch_query_value'>" + 
                    "<input type='text' aria-acs_qv='____aria-acs_qv___' id='id_value_input' value='__txt_init_value__' class='ui-widget-content ui-corner-all acs_default_gray acs_getv' />" +
                "</div>";
            var e_v_date = "" +
                "<div class='voch_query_value'>" + 
                    "<input type='text' aria-acs_qv='____aria-acs_qv___' class='input-elm ui-widget-content ui-corner-all acs_default_gray acs_getv' id='id_value_input' value='__txt_init_value__'/>" +
                "</div>";
            var e_v_select = "" +
                "<div class='voch_query_value'>" + 
                    "<select id='id_qcmp_select' class='ui-widget-content ui-corner-all acs_getv' aria-acs_qv='____aria-acs_qv___'>" + 
                        "__txt_v_select_option__" +
                    "</select>" + 
                "</div>";
            
            var acs_int_html = "" +
            "<div class='acs_item'>" + 
                e_key + e_comp_int + e_v_text + 
            "</div>" + 
            "<div class='acc_clear' style='height:5px;'></div>";

            var acs_date_html = "" +
            "<div class='acs_item'>" + 
                e_key + e_comp_int + e_v_date + 
            "</div>" + 
            "<div class='acc_clear' style='height:5px;'></div>";
            
            var acs_text_html = "" +
            "<div class='acs_item'>" + 
                e_key + e_comp_text + e_v_text + 
            "</div>" + 
            "<div class='acc_clear' style='height:5px;'></div>";
            
            var acs_select_html = "" +
            "<div class='acs_item'>" + 
                e_key + e_comp_select + e_v_select + 
            "</div>" + 
            "<div class='acc_clear' style='height:5px;'></div>";

            for(var i = 0; i < opts.model.length; ++i) {
                var mdl = opts.model[i];
                var def_comp;
                var r_html;
                if("int" == opts.model[i].search_type) {
                    def_comp = mdl.def_comp || "eq";
                    r_html = acs_int_html;
                } else if ("date" == opts.model[i].search_type) {
                    def_comp = mdl.def_comp || "gt";
                    r_html = acs_date_html;
                } else if ("text" == opts.model[i].search_type) {
                    def_comp = mdl.def_comp || "cn";
                    r_html = acs_text_html;
                } else if ("select" == opts.model[i].search_type) {
                    def_comp = mdl.def_comp || "cn";
                    r_html = acs_select_html.replace(/__txt_v_select_option__/, mdl.data_src);
                }
                var v_def_show = mdl.def_value || "不限";
                r_html = r_html.replace(/__txt_key__/, mdl.label)
                .replace(/__txt_init_value__/, v_def_show)
                .replace(/id_qcmp_select/, "id_qcmp_select_" + mdl.name + i)
                .replace(/id_value_input/, "id_value_input_" + mdl.name + i)
                .replace(/____aria-acs_qv___/, mdl.name);
                $(this).append(r_html);
                
                if("date" == mdl.search_type) {
                    $("#id_value_input_" + mdl.name + i).datepicker({
                        changeMonth: true,
                        changeYear: true,
                        dateFormat: "yy-mm-dd",
                        autoclose: true,
                        beforeShow: function() {
                            //$('#ui-datepicker-div').css("z-index", 500);
                        },
                        onSelect: function(dateText) {
                            //$("#id_value_input_" + mdl.name + i).blur();
                            //$(this).datepicker('hide');
                            //$(this).data("DateTimePicker").hide();
                            //$(".ui-datepicker").hide();
                        },
                        changeDate:function() {
                            //$(this).data("DateTimePicker").hide();
                            //$(this).datepicker('hide');
                        },
                    });
                
                //$("#id_value_input_" + mdl.name + i).focus(function() {console.log($(this).attr("id") + " was focus.");}).focusout();
                }

                $("#id_qcmp_select_" + mdl.name + i).find("[aria-alias='" + def_comp + "']").attr("selected", "selected");
                $("#id_value_input_" + mdl.name + i).off("change").on("change", function() {
                    if("不限" == $(this).val()) {
                        $(this).addClass("acs_default_gray");
                    } else if("" == $(this).val()) {
                        $(this).val("不限");
                        $(this).addClass("acs_default_gray");
                    } else {
                        $(this).removeClass("acs_default_gray");
                    }
                });
            }
            //$(this).css("color", opts.foreground);
        },
        acc_get_sql_text:function(options) {
            var ret = [];
            for(var i = 0; i < $(".acs_item").length; ++i) {
                var comp = $(".acs_item")[i];
                var val =  $(comp).find(".acs_getv").val();
                var cmp = $(comp).find(".acs_getcomp").val();
                var key_id = $(comp).find(".acs_getv").attr("id");
                var key = $(comp).find(".acs_getv").attr("aria-acs_qv");
                if("不限" != val) {
                    ret.push({kid:key_id, k:key, c:cmp, v:val});
                    console.log("key id=" + key_id +", " + key + " " + cmp + " " + val);
                }
            }
            return ret;
        },
    });
})(jQuery);